package com.example.crm.service.impl;

import com.example.crm.entity.Account;
import com.example.crm.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@Transactional
public class TokenServiceImpl implements TokenService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void saveToken(String token , Account account) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Account>(Account.class));
        redisTemplate.opsForValue().set(token,account,1200L, TimeUnit.MINUTES);
    }

    @Override
    public Object getToken(String token) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Account>(Account.class));
        return redisTemplate.opsForValue().get(token);
    }
}
